#!/usr/bin/perl

open(FILE, "preamble.txt");
my @lines = <FILE>;
close(FILE);
my $preamble = join("", @lines);

print "Prepending preamble to source files...\n";
preamble("\\.java\$", "/*", "*/");
preamble("\\.jsp\$", "<%--", "--%>");
preamble("\\.tag\$", "<%--", "--%>");
sub preamble {
  my ($pattern, $prefix, $suffix) = @_;
  $result = `find . | grep "$pattern"`;
  my @files = split("\n", $result);
  chomp(@files);
  foreach my $file (@files) {
    my $tmp = "$file.tmp";
    system("mv $file $tmp");
    open(OLD, "$tmp");
    my $shouldPrepend = 1;
    while (<OLD>) {
      if ($_ =~ /GNU/) {
        $shouldPrepend = 0;
      }
    }
    close(OLD);

    open(FILE, ">$file");
    if ($shouldPrepend) {
      print "  $file...";
      print FILE "$prefix\n";
      print FILE $preamble;
      print FILE "$suffix\n";
      print "Done.\n"; 
    }
    close(FILE);
    system("cat $tmp >> $file");
    unlink($tmp); 
  }
}
print "done.\n";
